In [1]:
import numpy as np
import os
from IPython.display import display
from matplotlib import pyplot as plt
from sklearn.svm import LinearSVC
%matplotlib inline

In [2]:
from importlib import reload
import naive_keras
reload(naive_keras)
from naive_keras import Sequential, Conv2D, MaxPooling2D, ZeroPadding2D, Dense

In [3]:
model = Sequential(Conv2D(5, input_shape=(None, 20, 20, 3)),
                    MaxPooling2D(),
                    ZeroPadding2D(),
                    Conv2D(5))

In [4]:
x = np.random.randn(10,20,20,3)
y = model.predict(x)

In [5]:
model.layers[0].save_memory = True
model.layers[-1].save_memory = True
y2 = model.predict(x)

In [6]:
np.allclose(y, y2)


Out[6]:
True

In [2]:
from importlib import reload
import naive_keras
reload(naive_keras)
from naive_keras import Sequential, Conv2D, MaxPooling2D, ZeroPadding2D

model = Sequential(ZeroPadding2D(input_shape=(None, 56, 56, 3)),
                   Conv2D(20),
                   ZeroPadding2D(),
                   Conv2D(20),
                   ZeroPadding2D(),
                   Conv2D(20),
                   ZeroPadding2D(),
                   Conv2D(20),
                   ZeroPadding2D(),
                   Conv2D(20),
                   ZeroPadding2D(),
                   Conv2D(20))

model.folder = '/home/spi/nbs/input/train/'

In [3]:
model.summary()


input_shape output_shape
<class 'naive_keras.ZeroPadding2D'> (56, 56, 3) (58, 58, 3)
<class 'naive_keras.Conv2D'> (58, 58, 3) (56, 56, 20)
<class 'naive_keras.ZeroPadding2D'> (56, 56, 20) (58, 58, 20)
<class 'naive_keras.Conv2D'> (58, 58, 20) (56, 56, 20)
<class 'naive_keras.ZeroPadding2D'> (56, 56, 20) (58, 58, 20)
<class 'naive_keras.Conv2D'> (58, 58, 20) (56, 56, 20)
<class 'naive_keras.ZeroPadding2D'> (56, 56, 20) (58, 58, 20)
<class 'naive_keras.Conv2D'> (58, 58, 20) (56, 56, 20)
<class 'naive_keras.ZeroPadding2D'> (56, 56, 20) (58, 58, 20)
<class 'naive_keras.Conv2D'> (58, 58, 20) (56, 56, 20)
<class 'naive_keras.ZeroPadding2D'> (56, 56, 20) (58, 58, 20)
<class 'naive_keras.Conv2D'> (58, 58, 20) (56, 56, 20)

In [4]:
svms = model.initialize_by_SVM('/home/spi/nbs/input/train/', batch_size=20)


100%|██████████| 20/20 [00:15<00:00,  1.25it/s, used_weights=94.7%]
  0%|          | 0/20 [00:00<?, ?it/s]
Elapsed 15.384s. Of these, 2.148s to load pics, 0.007s to predict, 13.195s to fit.
Used around 0.015GB of RAM

100%|██████████| 20/20 [00:42<00:00,  2.13s/it, used_weights=58.7%]
  0%|          | 0/20 [00:00<?, ?it/s]
Elapsed 42.772s. Of these, 2.101s to load pics, 0.990s to predict, 39.660s to fit.
Used around 0.101GB of RAM

100%|██████████| 20/20 [00:41<00:00,  2.04s/it, used_weights=58.5%]
  0%|          | 0/20 [00:00<?, ?it/s]
Elapsed 41.791s. Of these, 2.136s to load pics, 5.331s to predict, 34.301s to fit.
Used around 0.101GB of RAM

100%|██████████| 20/20 [00:45<00:00,  2.36s/it, used_weights=53.2%]
  0%|          | 0/20 [00:00<?, ?it/s]
Elapsed 45.300s. Of these, 2.153s to load pics, 9.988s to predict, 33.138s to fit.
Used around 0.101GB of RAM

100%|██████████| 20/20 [00:46<00:00,  2.24s/it, used_weights=55.6%]
  0%|          | 0/20 [00:00<?, ?it/s]
Elapsed 46.032s. Of these, 2.108s to load pics, 14.180s to predict, 29.723s to fit.
Used around 0.101GB of RAM

100%|██████████| 20/20 [00:50<00:00,  2.53s/it, used_weights=61.5%]
Elapsed 50.070s. Of these, 2.154s to load pics, 18.573s to predict, 29.321s to fit.
Used around 0.101GB of RAM



In [ ]:
np.expand_dims()

In [227]:
pics,labels = model.load_random_input(10, model.layers[0].input_shape[1:])

In [239]:
x = model.predict(pics, up_to_layer=model.layers[1])

In [228]:
y = model.predict(pics)

In [229]:
(y.reshape(-1,y.shape[-1])==0).mean(axis=0)


Out[229]:
array([ 0.62920918,  0.33459821,  0.34132653,  0.31964286,  0.40149872,
        0.34470663,  0.47423469,  0.32850765,  0.32040816,  0.36374362,
        0.51581633,  0.49840561,  0.54288903,  0.49461097,  0.40516582,
        0.32987883,  0.43137755,  0.52720026,  0.57104592,  0.37464923])

In [241]:
j = 3
plt.imshow(((pics[j]+2)*64).astype(np.uint8))


Out[241]:
<matplotlib.image.AxesImage at 0x7fd262d27940>

In [245]:
n = int(np.ceil(model.layers[1].output_shape[-1] / 5))
f, axes = plt.subplots(n,5,figsize=(15,3*n+2))
for i in range(model.layers[1].output_shape[-1]):
    axes[i//5,i%5].imshow(x[j,:,:,i])



In [248]:
plt.hist(np.log10(x.flatten()+1e-5))


Out[248]:
(array([ 310553.,     335.,    1151.,    3824.,   12608.,   40554.,
         106806.,  118827.,   30678.,    1864.]),
 array([-5.        , -4.48604998, -3.97209996, -3.45814995, -2.94419993,
        -2.43024991, -1.91629989, -1.40234987, -0.88839986, -0.37444984,
         0.13950018]),
 <a list of 10 Patch objects>)

In [237]:
n = int(np.ceil(model.layers[-1].output_shape[-1] / 5))
f, axes = plt.subplots(n,5,figsize=(15,3*n+2))
for i in range(model.layers[-1].output_shape[-1]):
    if n > 1:
        axes[i//5,i%5].imshow(y[j,:,:,i])
    else:
        axes[i].imshow(y[j,:,:,i])



In [249]:
plt.hist(np.log10(y.flatten()+1e-5))


Out[249]:
(array([ 268304.,     528.,    1446.,    3919.,   10341.,   27702.,
          75439.,  159252.,   77865.,    2404.]),
 array([-5.        , -4.58196507, -4.16393014, -3.74589522, -3.32786029,
        -2.90982536, -2.49179043, -2.07375551, -1.65572058, -1.23768565,
        -0.81965072]),
 <a list of 10 Patch objects>)

It does something...

Now should implement a fully connected layer on top with usual softmax and backpropagation to check if it really learns anything. As it's just one FC layer it's a quadratic problem, SGD will work fine.

Probably can simply use sklearn.linear_model.SGDClassifier with partial_fit...


In [9]:
reload(naive_keras)
from naive_keras import Dense, Sequential
linear = Sequential(Dense(2, activation='softmax', input_shape=(None, 56, 56, 3)))

In [10]:
linear.summary()


input_shape output_shape
<class 'naive_keras.Dense'> (56, 56, 3) (2,)

In [11]:
svms = linear.initialize_by_SVM(train_folder = '/home/spi/nbs/input/train/',
                                 valid_folder='/home/spi/nbs/input/valid/',
                               recompute=False, final_epochs=5)


  0%|          | 0/359 [00:00<?, ?it/s]
Predicting train folder
Predicting valid folder
Epoch 1. Starting to fit images to last layer
100%|██████████| 359/359 [00:17<00:00, 20.21it/s, train_score=61.9%]
100%|██████████| 31/31 [00:00<00:00, 53.00it/s, valid_score=53.9%]
  1%|          | 2/359 [00:00<00:28, 12.32it/s, train_score=63.0%]
Validation score: 53.9%
Epoch 2. Starting to fit images to last layer
100%|██████████| 359/359 [00:18<00:00, 20.39it/s, train_score=62.4%]
100%|██████████| 31/31 [00:00<00:00, 50.83it/s, valid_score=55.3%]
  1%|          | 2/359 [00:00<00:29, 12.20it/s, train_score=62.0%]
Validation score: 55.3%
Epoch 3. Starting to fit images to last layer
100%|██████████| 359/359 [00:18<00:00, 19.90it/s, train_score=63.5%]
100%|██████████| 31/31 [00:00<00:00, 54.10it/s, valid_score=51.6%]
  1%|          | 2/359 [00:00<00:28, 12.63it/s, train_score=75.0%]
Validation score: 51.6%
Epoch 4. Starting to fit images to last layer
100%|██████████| 359/359 [00:17<00:00, 20.26it/s, train_score=63.9%]
100%|██████████| 31/31 [00:00<00:00, 53.86it/s, valid_score=53.3%]
  1%|          | 2/359 [00:00<00:31, 11.44it/s, train_score=65.1%]
Validation score: 53.3%
Epoch 5. Starting to fit images to last layer
100%|██████████| 359/359 [00:17<00:00, 20.31it/s, train_score=64.6%]
100%|██████████| 31/31 [00:00<00:00, 52.06it/s, valid_score=51.6%]
Validation score: 51.6%


In [3]:
reload(naive_keras)
from naive_keras import Sequential, Dense
two_layers = Sequential(Dense(10, input_shape=(None, 56, 56, 3)),
                       Dense(2, activation='softmax'))

In [4]:
two_layers.summary()


input_shape output_shape
<class 'naive_keras.Dense'> (56, 56, 3) (10,)
<class 'naive_keras.Dense'> (10,) (2,)

In [5]:
svms = two_layers.initialize_by_SVM(train_folder = '/home/spi/nbs/input/train/',
                                 valid_folder='/home/spi/nbs/input/valid/')


100%|██████████| 10/10 [00:10<00:00,  1.03it/s, used_weights=20.4%]
  1%|          | 136/11500 [00:00<00:08, 1356.39it/s]
Elapsed 10.482s. Of these, 1.141s to load pics, 0.000s to predict, 9.327s to fit.
Used around 0.005GB of RAM

Predicting train folder
Predicting and saving dogs
100%|██████████| 11500/11500 [00:08<00:00, 1413.39it/s]
  1%|          | 61/11500 [00:00<00:18, 608.53it/s]
Predicting and saving cats
100%|██████████| 11500/11500 [00:08<00:00, 1350.37it/s]
 13%|█▎        | 132/1000 [00:00<00:00, 1315.65it/s]
Predicting valid folder
Predicting and saving dogs
100%|██████████| 1000/1000 [00:00<00:00, 1018.86it/s]
 13%|█▎        | 131/1000 [00:00<00:00, 1308.64it/s]
Predicting and saving cats
100%|██████████| 1000/1000 [00:00<00:00, 1392.15it/s]
  1%|          | 2/359 [00:00<00:31, 11.18it/s, train_score=52.7%]
Epoch 1. Starting to fit images to last layer
100%|██████████| 359/359 [00:15<00:00, 25.14it/s, train_score=55.5%]
100%|██████████| 31/31 [00:00<00:00, 73.37it/s, valid_score=50.5%]
Validation score: 50.5%


In [26]:
two_layers.layers[1].input_shape


Out[26]:
(None, 100)

Let's try...


In [7]:
from importlib import reload
import naive_keras
reload(naive_keras)
from naive_keras import Sequential, Conv2D, MaxPooling2D, ZeroPadding2D, Dense

model = Sequential(ZeroPadding2D(input_shape=(None, 56, 56, 3)),
                   Conv2D(40),
                   ZeroPadding2D(),
                   Conv2D(40),
                   MaxPooling2D(),
                   ZeroPadding2D(),
                   Conv2D(60),
                   ZeroPadding2D(),
                   Conv2D(60),
                   MaxPooling2D(),
                   ZeroPadding2D(),
                   Conv2D(80),
                   ZeroPadding2D(),
                   Conv2D(80),
                   MaxPooling2D(),
                   Dense(2, activation='softmax')
                  )

model.folder = '/home/spi/nbs/input/train/'

In [8]:
model.summary()


input_shape output_shape
<class 'naive_keras.ZeroPadding2D'> (56, 56, 3) (58, 58, 3)
<class 'naive_keras.Conv2D'> (58, 58, 3) (56, 56, 40)
<class 'naive_keras.ZeroPadding2D'> (56, 56, 40) (58, 58, 40)
<class 'naive_keras.Conv2D'> (58, 58, 40) (56, 56, 40)
<class 'naive_keras.MaxPooling2D'> (56, 56, 40) (28, 28, 40)
<class 'naive_keras.ZeroPadding2D'> (28, 28, 40) (30, 30, 40)
<class 'naive_keras.Conv2D'> (30, 30, 40) (28, 28, 60)
<class 'naive_keras.ZeroPadding2D'> (28, 28, 60) (30, 30, 60)
<class 'naive_keras.Conv2D'> (30, 30, 60) (28, 28, 60)
<class 'naive_keras.MaxPooling2D'> (28, 28, 60) (14, 14, 60)
<class 'naive_keras.ZeroPadding2D'> (14, 14, 60) (16, 16, 60)
<class 'naive_keras.Conv2D'> (16, 16, 60) (14, 14, 80)
<class 'naive_keras.ZeroPadding2D'> (14, 14, 80) (16, 16, 80)
<class 'naive_keras.Conv2D'> (16, 16, 80) (14, 14, 80)
<class 'naive_keras.MaxPooling2D'> (14, 14, 80) (7, 7, 80)
<class 'naive_keras.Dense'> (7, 7, 80) (2,)

In [9]:
svms = model.initialize_by_SVM(train_folder = '/home/spi/nbs/input/train/',
                                 valid_folder='/home/spi/nbs/input/valid/',
                              final_epochs=5)


100%|██████████| 40/40 [01:32<00:00,  2.39s/it, used_weights=96.1%]
  0%|          | 0/40 [00:00<?, ?it/s]
Elapsed 92.802s. Of these, 4.548s to load pics, 0.036s to predict, 88.168s to fit.
Used around 0.049GB of RAM

100%|██████████| 40/40 [15:56<00:00, 24.55s/it, used_weights=70.3%]
  0%|          | 0/60 [00:00<?, ?it/s]
Elapsed 956.669s. Of these, 4.861s to load pics, 13.258s to predict, 938.503s to fit.
Used around 0.647GB of RAM

100%|██████████| 60/60 [06:01<00:00,  6.53s/it, used_weights=47.2%]
  0%|          | 0/60 [00:00<?, ?it/s]
Elapsed 361.709s. Of these, 7.027s to load pics, 137.253s to predict, 217.367s to fit.
Used around 0.163GB of RAM

100%|██████████| 60/60 [07:40<00:00,  8.24s/it, used_weights=41.2%]
  0%|          | 0/80 [00:00<?, ?it/s]
Elapsed 460.334s. Of these, 7.351s to load pics, 179.573s to predict, 273.345s to fit.
Used around 0.244GB of RAM

100%|██████████| 80/80 [07:17<00:00,  5.76s/it, used_weights=41.7%]
  0%|          | 0/80 [00:00<?, ?it/s]
Elapsed 437.775s. Of these, 9.753s to load pics, 357.014s to predict, 70.923s to fit.
Used around 0.062GB of RAM

100%|██████████| 80/80 [08:21<00:00,  6.42s/it, used_weights=38.0%]
  0%|          | 0/11500 [00:00<?, ?it/s]
Elapsed 501.598s. Of these, 10.420s to load pics, 398.620s to predict, 92.463s to fit.
Used around 0.083GB of RAM

Predicting train folder
Predicting and saving dogs
100%|██████████| 11500/11500 [15:13<00:00, 12.62it/s]
  0%|          | 2/11500 [00:00<16:27, 11.65it/s]
Predicting and saving cats
100%|██████████| 11500/11500 [14:58<00:00, 12.17it/s]
  0%|          | 2/1000 [00:00<01:21, 12.17it/s]
Predicting valid folder
Predicting and saving dogs
100%|██████████| 1000/1000 [01:20<00:00, 12.33it/s]
  0%|          | 2/1000 [00:00<01:28, 11.30it/s]
Predicting and saving cats
100%|██████████| 1000/1000 [01:22<00:00, 12.49it/s]
  1%|          | 3/359 [00:00<00:23, 15.48it/s, train_score=65.6%]
Epoch 1. Starting to fit images to last layer
100%|██████████| 359/359 [00:18<00:00, 19.68it/s, train_score=68.0%]
100%|██████████| 31/31 [00:00<00:00, 46.93it/s, valid_score=56.1%]
  1%|          | 2/359 [00:00<00:29, 11.97it/s, train_score=70.3%]
Validation score: 56.1%
Epoch 2. Starting to fit images to last layer
100%|██████████| 359/359 [00:17<00:00, 19.31it/s, train_score=69.6%]
100%|██████████| 31/31 [00:00<00:00, 52.24it/s, valid_score=62.3%]
  1%|          | 2/359 [00:00<00:27, 12.88it/s, train_score=71.9%]
Validation score: 62.3%
Epoch 3. Starting to fit images to last layer
100%|██████████| 359/359 [00:17<00:00, 20.45it/s, train_score=70.8%]
100%|██████████| 31/31 [00:00<00:00, 56.65it/s, valid_score=62.3%]
  1%|          | 2/359 [00:00<00:27, 13.12it/s, train_score=67.2%]
Validation score: 62.3%
Epoch 4. Starting to fit images to last layer
100%|██████████| 359/359 [00:17<00:00, 20.46it/s, train_score=70.2%]
100%|██████████| 31/31 [00:00<00:00, 53.27it/s, valid_score=63.3%]
  1%|          | 2/359 [00:00<00:28, 12.43it/s, train_score=69.3%]
Validation score: 63.3%
Epoch 5. Starting to fit images to last layer
100%|██████████| 359/359 [00:17<00:00, 20.53it/s, train_score=71.6%]
100%|██████████| 31/31 [00:00<00:00, 46.47it/s, valid_score=53.6%]
Validation score: 53.6%


In [12]:
from naive_keras import save_array

In [50]:
reload(naive_keras)
from naive_keras import save_weights, fit_only_last, Sequential, Dense, Conv2D, ZeroPadding2D, MaxPooling2D

In [51]:
model2 = Sequential(ZeroPadding2D(input_shape=(None, 56, 56, 3)),
                   Conv2D(40),
                   ZeroPadding2D(),
                   Conv2D(40),
                   MaxPooling2D(),
                   ZeroPadding2D(),
                   Conv2D(60),
                   ZeroPadding2D(),
                   Conv2D(60),
                   MaxPooling2D(),
                   ZeroPadding2D(),
                   Conv2D(80),
                   ZeroPadding2D(),
                   Conv2D(80),
                   MaxPooling2D(),
                   Dense(2, activation='softmax')
                  )

In [52]:
model2.load_weights('/home/spi/nbs/model_weights/first_cnn_with_svm')

In [53]:
model2.fit_only_last(train_folder = '/home/spi/nbs/input/train/',
              valid_folder='/home/spi/nbs/input/valid/', epochs=50, recompute=False)


Predicting train folder
  1%|          | 2/359 [00:00<00:20, 17.34it/s, train_score=60.2%]
Predicting valid folder
Epoch 1. Starting to fit images to last layer
100%|██████████| 359/359 [00:21<00:00, 14.01it/s, train_score=68.5%]
100%|██████████| 31/31 [00:00<00:00, 34.50it/s, valid_score=63.6%]
  1%|          | 2/359 [00:00<00:30, 11.63it/s, train_score=70.8%]
Validation score: 63.6%
Epoch 2. Starting to fit images to last layer
100%|██████████| 359/359 [00:20<00:00, 17.41it/s, train_score=69.2%]
100%|██████████| 31/31 [00:00<00:00, 43.73it/s, valid_score=57.8%]
  1%|          | 2/359 [00:00<00:30, 11.58it/s, train_score=64.1%]
Validation score: 57.8%
Epoch 3. Starting to fit images to last layer
100%|██████████| 359/359 [00:19<00:00, 18.64it/s, train_score=71.1%]
100%|██████████| 31/31 [00:00<00:00, 47.63it/s, valid_score=61.7%]
  1%|          | 2/359 [00:00<00:30, 11.84it/s, train_score=65.6%]
Validation score: 61.7%
Epoch 4. Starting to fit images to last layer
100%|██████████| 359/359 [00:18<00:00, 13.72it/s, train_score=71.3%]
100%|██████████| 31/31 [00:00<00:00, 41.53it/s, valid_score=62.7%]
  1%|          | 2/359 [00:00<00:29, 11.91it/s, train_score=69.3%]
Validation score: 62.7%
Epoch 5. Starting to fit images to last layer
100%|██████████| 359/359 [00:18<00:00, 20.62it/s, train_score=71.1%]
100%|██████████| 31/31 [00:00<00:00, 45.45it/s, valid_score=65.0%]
  1%|          | 2/359 [00:00<00:30, 11.69it/s, train_score=69.8%]
Validation score: 65.0%
Epoch 6. Starting to fit images to last layer
100%|██████████| 359/359 [00:17<00:00, 20.41it/s, train_score=71.7%]
100%|██████████| 31/31 [00:00<00:00, 43.17it/s, valid_score=60.9%]
  1%|          | 2/359 [00:00<00:28, 12.71it/s, train_score=74.5%]
Validation score: 60.9%
Epoch 7. Starting to fit images to last layer
100%|██████████| 359/359 [00:17<00:00, 20.52it/s, train_score=72.1%]
100%|██████████| 31/31 [00:00<00:00, 44.74it/s, valid_score=62.4%]
  1%|          | 2/359 [00:00<00:28, 12.59it/s, train_score=70.8%]
Validation score: 62.4%
Epoch 8. Starting to fit images to last layer
100%|██████████| 359/359 [00:18<00:00, 20.23it/s, train_score=72.1%]
100%|██████████| 31/31 [00:00<00:00, 49.00it/s, valid_score=61.6%]
  1%|          | 3/359 [00:00<00:17, 20.63it/s, train_score=69.8%]
Validation score: 61.6%
Epoch 9. Starting to fit images to last layer
100%|██████████| 359/359 [00:18<00:00, 20.12it/s, train_score=72.3%]
100%|██████████| 31/31 [00:00<00:00, 46.58it/s, valid_score=61.6%]
  1%|          | 2/359 [00:00<00:26, 13.25it/s, train_score=65.6%]
Validation score: 61.6%
Epoch 10. Starting to fit images to last layer
100%|██████████| 359/359 [00:18<00:00, 17.79it/s, train_score=72.6%]
100%|██████████| 31/31 [00:00<00:00, 53.10it/s, valid_score=62.8%]
  1%|          | 2/359 [00:00<00:28, 12.62it/s, train_score=76.0%]
Validation score: 62.8%
Epoch 11. Starting to fit images to last layer
100%|██████████| 359/359 [00:19<00:00, 18.29it/s, train_score=73.1%]
100%|██████████| 31/31 [00:00<00:00, 51.34it/s, valid_score=63.8%]
  1%|          | 2/359 [00:00<00:28, 12.72it/s, train_score=62.5%]
Validation score: 63.8%
Epoch 12. Starting to fit images to last layer
100%|██████████| 359/359 [00:18<00:00, 21.36it/s, train_score=73.0%]
100%|██████████| 31/31 [00:00<00:00, 54.98it/s, valid_score=64.5%]
  1%|          | 3/359 [00:00<00:16, 21.03it/s, train_score=65.6%]
Validation score: 64.5%
Epoch 13. Starting to fit images to last layer
100%|██████████| 359/359 [00:17<00:00, 20.61it/s, train_score=73.3%]
100%|██████████| 31/31 [00:00<00:00, 58.85it/s, valid_score=65.5%]
  1%|          | 3/359 [00:00<00:21, 16.51it/s, train_score=77.7%]
Validation score: 65.5%
Epoch 14. Starting to fit images to last layer
100%|██████████| 359/359 [00:17<00:00, 20.79it/s, train_score=73.9%]
100%|██████████| 31/31 [00:00<00:00, 59.90it/s, valid_score=58.7%]
  1%|          | 3/359 [00:00<00:22, 16.08it/s, train_score=79.7%]
Validation score: 58.7%
Epoch 15. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.25it/s, train_score=72.7%]
100%|██████████| 31/31 [00:00<00:00, 58.44it/s, valid_score=65.1%]
  1%|          | 3/359 [00:00<00:22, 16.06it/s, train_score=73.0%]
Validation score: 65.1%
Epoch 16. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.62it/s, train_score=72.8%]
100%|██████████| 31/31 [00:00<00:00, 58.12it/s, valid_score=62.2%]
  1%|          | 3/359 [00:00<00:16, 21.97it/s, train_score=66.1%]
Validation score: 62.2%
Epoch 17. Starting to fit images to last layer
100%|██████████| 359/359 [00:19<00:00, 18.80it/s, train_score=73.9%]
100%|██████████| 31/31 [00:00<00:00, 59.49it/s, valid_score=66.8%]
  1%|          | 3/359 [00:00<00:21, 16.49it/s, train_score=71.9%]
Validation score: 66.8%
Epoch 18. Starting to fit images to last layer
100%|██████████| 359/359 [00:17<00:00, 21.79it/s, train_score=74.0%]
100%|██████████| 31/31 [00:00<00:00, 59.32it/s, valid_score=60.1%]
  1%|          | 3/359 [00:00<00:21, 16.59it/s, train_score=71.9%]
Validation score: 60.1%
Epoch 19. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.59it/s, train_score=73.3%]
100%|██████████| 31/31 [00:00<00:00, 58.92it/s, valid_score=65.6%]
  1%|          | 3/359 [00:00<00:21, 16.68it/s, train_score=71.5%]
Validation score: 65.6%
Epoch 20. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.29it/s, train_score=74.4%]
100%|██████████| 31/31 [00:00<00:00, 59.22it/s, valid_score=67.4%]
  1%|          | 3/359 [00:00<00:21, 16.80it/s, train_score=76.6%]
Validation score: 67.4%
Epoch 21. Starting to fit images to last layer
100%|██████████| 359/359 [00:19<00:00, 20.68it/s, train_score=73.8%]
100%|██████████| 31/31 [00:00<00:00, 55.27it/s, valid_score=64.0%]
  1%|          | 3/359 [00:00<00:22, 15.78it/s, train_score=72.3%]
Validation score: 64.0%
Epoch 22. Starting to fit images to last layer
100%|██████████| 359/359 [00:18<00:00, 21.69it/s, train_score=73.3%]
100%|██████████| 31/31 [00:00<00:00, 55.21it/s, valid_score=63.1%]
  1%|          | 2/359 [00:00<00:31, 11.45it/s, train_score=76.6%]
Validation score: 63.1%
Epoch 23. Starting to fit images to last layer
100%|██████████| 359/359 [00:17<00:00, 21.04it/s, train_score=74.4%]
100%|██████████| 31/31 [00:00<00:00, 57.07it/s, valid_score=62.5%]
  1%|          | 3/359 [00:00<00:21, 16.27it/s, train_score=73.8%]
Validation score: 62.5%
Epoch 24. Starting to fit images to last layer
100%|██████████| 359/359 [00:19<00:00, 18.68it/s, train_score=73.6%]
100%|██████████| 31/31 [00:00<00:00, 42.92it/s, valid_score=63.8%]
  1%|          | 3/359 [00:00<00:22, 16.06it/s, train_score=76.6%]
Validation score: 63.8%
Epoch 25. Starting to fit images to last layer
100%|██████████| 359/359 [00:18<00:00, 18.96it/s, train_score=74.1%]
100%|██████████| 31/31 [00:00<00:00, 47.68it/s, valid_score=61.3%]
  1%|          | 3/359 [00:00<00:22, 15.71it/s, train_score=74.2%]
Validation score: 61.3%
Epoch 26. Starting to fit images to last layer
100%|██████████| 359/359 [00:18<00:00, 20.09it/s, train_score=73.5%]
100%|██████████| 31/31 [00:00<00:00, 39.43it/s, valid_score=58.7%]
  1%|          | 2/359 [00:00<00:31, 11.19it/s, train_score=71.4%]
Validation score: 58.7%
Epoch 27. Starting to fit images to last layer
100%|██████████| 359/359 [00:19<00:00, 16.11it/s, train_score=73.8%]
100%|██████████| 31/31 [00:00<00:00, 49.94it/s, valid_score=60.8%]
  1%|          | 2/359 [00:00<00:28, 12.64it/s, train_score=69.3%]
Validation score: 60.8%
Epoch 28. Starting to fit images to last layer
100%|██████████| 359/359 [00:17<00:00, 20.23it/s, train_score=74.3%]
100%|██████████| 31/31 [00:00<00:00, 50.93it/s, valid_score=63.5%]
  1%|          | 2/359 [00:00<00:26, 13.43it/s, train_score=68.2%]
Validation score: 63.5%
Epoch 29. Starting to fit images to last layer
100%|██████████| 359/359 [00:17<00:00, 18.86it/s, train_score=74.0%]
100%|██████████| 31/31 [00:00<00:00, 56.23it/s, valid_score=64.1%]
  1%|          | 3/359 [00:00<00:23, 15.45it/s, train_score=75.0%]
Validation score: 64.1%
Epoch 30. Starting to fit images to last layer
100%|██████████| 359/359 [00:17<00:00, 20.37it/s, train_score=74.8%]
100%|██████████| 31/31 [00:00<00:00, 58.81it/s, valid_score=63.9%]
  1%|          | 3/359 [00:00<00:21, 16.84it/s, train_score=70.3%]
Validation score: 63.9%
Epoch 31. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.61it/s, train_score=74.2%]
100%|██████████| 31/31 [00:00<00:00, 58.48it/s, valid_score=64.5%]
  1%|          | 3/359 [00:00<00:21, 16.45it/s, train_score=74.2%]
Validation score: 64.5%
Epoch 32. Starting to fit images to last layer
100%|██████████| 359/359 [00:17<00:00, 20.64it/s, train_score=74.6%]
100%|██████████| 31/31 [00:00<00:00, 58.79it/s, valid_score=57.6%]
  1%|          | 3/359 [00:00<00:21, 16.27it/s, train_score=71.5%]
Validation score: 57.6%
Epoch 33. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.42it/s, train_score=74.6%]
100%|██████████| 31/31 [00:00<00:00, 58.32it/s, valid_score=55.5%]
  1%|          | 3/359 [00:00<00:21, 16.44it/s, train_score=77.7%]
Validation score: 55.5%
Epoch 34. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.61it/s, train_score=75.0%]
100%|██████████| 31/31 [00:00<00:00, 59.13it/s, valid_score=62.2%]
  1%|          | 3/359 [00:00<00:21, 16.45it/s, train_score=69.9%]
Validation score: 62.2%
Epoch 35. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.64it/s, train_score=74.1%]
100%|██████████| 31/31 [00:00<00:00, 58.53it/s, valid_score=65.2%]
  1%|          | 3/359 [00:00<00:21, 16.82it/s, train_score=71.1%]
Validation score: 65.2%
Epoch 36. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.66it/s, train_score=74.6%]
100%|██████████| 31/31 [00:00<00:00, 59.00it/s, valid_score=62.1%]
  1%|          | 3/359 [00:00<00:21, 16.45it/s, train_score=77.3%]
Validation score: 62.1%
Epoch 37. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.43it/s, train_score=73.8%]
100%|██████████| 31/31 [00:00<00:00, 59.04it/s, valid_score=65.8%]
  1%|          | 3/359 [00:00<00:21, 16.42it/s, train_score=71.1%]
Validation score: 65.8%
Epoch 38. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.74it/s, train_score=74.7%]
100%|██████████| 31/31 [00:00<00:00, 58.78it/s, valid_score=63.0%]
  1%|          | 3/359 [00:00<00:21, 16.37it/s, train_score=74.2%]
Validation score: 63.0%
Epoch 39. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.52it/s, train_score=73.9%]
100%|██████████| 31/31 [00:00<00:00, 58.40it/s, valid_score=53.3%]
  1%|          | 3/359 [00:00<00:21, 16.20it/s, train_score=79.3%]
Validation score: 53.3%
Epoch 40. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.49it/s, train_score=74.7%]
100%|██████████| 31/31 [00:00<00:00, 59.41it/s, valid_score=66.3%]
  1%|          | 3/359 [00:00<00:21, 16.20it/s, train_score=76.2%]
Validation score: 66.3%
Epoch 41. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.64it/s, train_score=74.3%]
100%|██████████| 31/31 [00:00<00:00, 58.53it/s, valid_score=57.2%]
  1%|          | 3/359 [00:00<00:21, 16.39it/s, train_score=76.6%]
Validation score: 57.2%
Epoch 42. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.77it/s, train_score=75.1%]
100%|██████████| 31/31 [00:00<00:00, 58.28it/s, valid_score=65.8%]
  1%|          | 3/359 [00:00<00:21, 16.65it/s, train_score=80.1%]
Validation score: 65.8%
Epoch 43. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.71it/s, train_score=75.3%]
100%|██████████| 31/31 [00:00<00:00, 59.37it/s, valid_score=63.9%]
  1%|          | 3/359 [00:00<00:21, 16.77it/s, train_score=77.0%]
Validation score: 63.9%
Epoch 44. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.61it/s, train_score=74.9%]
100%|██████████| 31/31 [00:00<00:00, 59.12it/s, valid_score=63.3%]
  1%|          | 3/359 [00:00<00:21, 16.88it/s, train_score=82.0%]
Validation score: 63.3%
Epoch 45. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.69it/s, train_score=74.5%]
100%|██████████| 31/31 [00:00<00:00, 58.55it/s, valid_score=66.1%]
  1%|          | 3/359 [00:00<00:21, 16.85it/s, train_score=73.4%]
Validation score: 66.1%
Epoch 46. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.78it/s, train_score=75.6%]
100%|██████████| 31/31 [00:00<00:00, 58.91it/s, valid_score=64.1%]
  1%|          | 3/359 [00:00<00:21, 16.69it/s, train_score=76.6%]
Validation score: 64.1%
Epoch 47. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.58it/s, train_score=74.9%]
100%|██████████| 31/31 [00:00<00:00, 58.99it/s, valid_score=63.5%]
  1%|          | 3/359 [00:00<00:21, 16.32it/s, train_score=71.5%]
Validation score: 63.5%
Epoch 48. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.67it/s, train_score=75.1%]
100%|██████████| 31/31 [00:00<00:00, 58.96it/s, valid_score=60.9%]
  1%|          | 3/359 [00:00<00:21, 16.87it/s, train_score=78.5%]
Validation score: 60.9%
Epoch 49. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.80it/s, train_score=74.4%]
100%|██████████| 31/31 [00:00<00:00, 58.69it/s, valid_score=65.7%]
  1%|          | 3/359 [00:00<00:21, 16.48it/s, train_score=73.0%]
Validation score: 65.7%
Epoch 50. Starting to fit images to last layer
100%|██████████| 359/359 [00:16<00:00, 21.49it/s, train_score=74.5%]
100%|██████████| 31/31 [00:00<00:00, 58.36it/s, valid_score=66.8%]
Validation score: 66.8%

Out[53]:
[SGDClassifier(alpha=0.0001, average=False, class_weight=None, epsilon=0.1,
        eta0=0.0, fit_intercept=True, l1_ratio=0.15,
        learning_rate='optimal', loss='hinge', max_iter=1000, n_iter=None,
        n_jobs=1, penalty='l2', power_t=0.5, random_state=None,
        shuffle=True, tol=None, verbose=0, warm_start=False)]

In [25]:
m = model
for l in m.layers:
    if isinstance(l, Dense) and [l1 for l1 in m.layers if isinstance(l1, Dense)][::-1].index(l) == 0:
        print(l)


<naive_keras.Dense object at 0x7f8d357e36a0>

In [54]:
model2.save_weights('/home/spi/nbs/model_weights/second_cnn_with_svm')

In [16]:
space = 0
for l in model.layers:
    try:
        l.weights
    except:
        pass
    else:
        space += l.weights[0].size + l.weights[1].size

In [18]:
space * 8 /1e6


Out[18]:
1.427856